/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entitas;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import jpa.exceptions.NonexistentEntityException;
/**
 *
 * @author Muhammad Fachrurrozy
 */
public class DaftarRequestBuku {
 /* menghubungkan dengan database sesuai dengan
     * konfigurasi pada persistence unit
     */
    public DaftarRequestBuku() {
        emf = Persistence.createEntityManagerFactory("persistence");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    /* method yang digunakan untuk mengecek keberadaan satu baris
     * pada tabel requestbuku berdasarkan parameter judulBuku
     */
    public boolean check(String judulBuku) {
        boolean result = false;
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT count(o) FROM RequestBuku AS o WHERE o.judulBuku=:drb");
            q.setParameter("drb", judulBuku);
            int jumlahUser = ((Long) q.getSingleResult()).intValue();
            if (jumlahUser > 0) {
                result = true;
            }
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return result;
    }

    /* method untuk mengambil satu baris buku
     * berdasarkan parameter judulBuku
     */
    public RequestBuku getRequestBuku(String judulBuku) {
        RequestBuku requestbuku = null;
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT object(o) FROM RequestBuku AS o WHERE o.judulBuku=:drb");
            q.setParameter("drb", judulBuku);
            requestbuku = (RequestBuku) q.getSingleResult();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return requestbuku;
    }

    /* method untuk mengambil semua baris dari tabel requestbuku
     */
    public List<RequestBuku> getRequestBooks() {
        List<RequestBuku> requestbuku = new ArrayList<RequestBuku>();

        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT object(o) FROM RequestBuku AS o");
            requestbuku = q.getResultList();

        } finally {
            if (em != null) {
                em.close();
            }
        }
        return requestbuku;
    }

    /* method untuk mengganti nilai atribut pada baris yang sudah ada pada tabel requestbuku
     */
    public void editRequestBuku(RequestBuku requestbuku) {
        EntityManager em = getEntityManager();
        em.getTransaction().begin();
        try {
            em.merge(requestbuku);
            em.getTransaction().commit();
        } catch (Exception e) {
            em.getTransaction().rollback();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    /* method untuk memasukkan baris baru ke dalam tabel requestbuku
     */
    public void createRequestBuku(RequestBuku requestbuku) {
        EntityManager em = getEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(requestbuku);
            em.getTransaction().commit();
        } catch (Exception e) {
            em.getTransaction().rollback();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    /* method untuk menghapus satu baris pada tabel requestbuku
     */
    public void deleteRequestBuku(int id) throws NonexistentEntityException {
        EntityManager em = getEntityManager();
        em.getTransaction().begin();
        try {
            RequestBuku requestbuku;
            try {
                requestbuku = em.getReference(RequestBuku.class, id);
                requestbuku.getIdRequestBuku();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The user with id " + id + " no longer exists.", enfe);
            }
            em.remove(requestbuku);
            em.getTransaction().commit();
        } catch (Exception e) {
            em.getTransaction().rollback();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

}

